﻿namespace OleDbDAL.Sales
{
    using DBUtility;
    using IDal.Sales;
    using Model.Sales;
    using System;
    using System.Data;
    using System.Text;

    public class TestDunkApp : ITestDunk
    {
        private string dbname = "Systems";

        public void Add(TestDunkEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into T_TestDunk(");
            builder.Append("ChainNum,TestDunkNum,GuestId,GuestName,UserId,CreateTime,Prices,IsSucceed,IsCheck,CheckMan,CheckTime,Remark");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("" + model.ChainNum + ",");
            builder.Append("'" + model.TestDunkNum + "',");
            builder.Append("" + model.GuestId + ",");
            builder.Append("'" + model.GuestName + "',");
            builder.Append("" + model.UserId + ",");
            builder.Append("'" + model.CreateTime + "',");
            builder.Append("" + model.Prices + ",");
            builder.Append("" + model.IsSucceed + ",");
            builder.Append("" + model.IsCheck + ",");
            builder.Append("" + model.CheckMan + ",");
            builder.Append("'" + model.CheckTime + "',");
            builder.Append("'" + model.Remark + "'");
            builder.Append(")");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Delete(int TestDunkId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from T_TestDunk ");
            builder.Append(" where TestDunkId=" + TestDunkId);
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_TestDunk ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by TestDunkId ");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public int GetMaxId(string swhere)
        {
            return DbHelperOledb.GetMaxID(this.dbname, "TestDunkId", "T_TestDunk", swhere);
        }

        public TestDunkEnt GetModel(int TestDunkId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select TestDunkId,ChainNum,TestDunkNum,GuestId,GuestName,UserId,CreateTime,Prices,IsSucceed,IsCheck,CheckMan,CheckTime,Remark from T_TestDunk ");
            builder.Append(" where TestDunkId=" + TestDunkId);
            TestDunkEnt ent = new TestDunkEnt();
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            ent.TestDunkId = TestDunkId;
            if (table.Rows.Count > 0)
            {
                if (table.Rows[0]["ChainNum"].ToString() != "")
                {
                    ent.ChainNum = int.Parse(table.Rows[0]["ChainNum"].ToString());
                }
                ent.TestDunkNum = table.Rows[0]["TestDunkNum"].ToString();
                if (table.Rows[0]["GuestId"].ToString() != "")
                {
                    ent.GuestId = int.Parse(table.Rows[0]["GuestId"].ToString());
                }
                ent.GuestName = table.Rows[0]["GuestName"].ToString();
                if (table.Rows[0]["UserId"].ToString() != "")
                {
                    ent.UserId = int.Parse(table.Rows[0]["UserId"].ToString());
                }
                if (table.Rows[0]["CreateTime"].ToString() != "")
                {
                    ent.CreateTime = DateTime.Parse(table.Rows[0]["CreateTime"].ToString());
                }
                if (table.Rows[0]["Prices"].ToString() != "")
                {
                    ent.Prices = decimal.Parse(table.Rows[0]["Prices"].ToString());
                }
                if (table.Rows[0]["IsSucceed"].ToString() != "")
                {
                    ent.IsSucceed = int.Parse(table.Rows[0]["IsSucceed"].ToString());
                }
                if (table.Rows[0]["IsCheck"].ToString() != "")
                {
                    ent.IsCheck = int.Parse(table.Rows[0]["IsCheck"].ToString());
                }
                if (table.Rows[0]["CheckMan"].ToString() != "")
                {
                    ent.CheckMan = int.Parse(table.Rows[0]["CheckMan"].ToString());
                }
                if (table.Rows[0]["CheckTime"].ToString() != "")
                {
                    ent.CheckTime = DateTime.Parse(table.Rows[0]["CheckTime"].ToString());
                }
                ent.Remark = table.Rows[0]["Remark"].ToString();
                return ent;
            }
            return null;
        }

        public DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            string orderString = "";
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_TestDunk ");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), strWhere, orderString, out pageCount, out recordCount, "TestDunkId");
        }

        public void IsCheckAndOutStock(TestDunkEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_TestDunk set ");
            builder.Append("IsCheck=" + model.IsCheck + ",");
            builder.Append("CheckMan=" + model.CheckMan + ",");
            builder.Append("CheckTime='" + model.CheckTime + "'");
            builder.Append(" where TestDunkId=" + model.TestDunkId + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Update(TestDunkEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_TestDunk set ");
            builder.Append("ChainNum=" + model.ChainNum + ",");
            builder.Append("TestDunkNum='" + model.TestDunkNum + "',");
            builder.Append("GuestId=" + model.GuestId + ",");
            builder.Append("GuestName='" + model.GuestName + "',");
            builder.Append("UserId=" + model.UserId + ",");
            builder.Append("CreateTime='" + model.CreateTime + "',");
            builder.Append("Prices=" + model.Prices + ",");
            builder.Append("IsSucceed=" + model.IsSucceed + ",");
            builder.Append("IsCheck=" + model.IsCheck + ",");
            builder.Append("CheckMan=" + model.CheckMan + ",");
            builder.Append("CheckTime='" + model.CheckTime + "',");
            builder.Append("Remark='" + model.Remark + "'");
            builder.Append(" where TestDunkId=" + model.TestDunkId + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }
    }
}

